package cn.meizhu.pet.service.impl;

import cn.meizhu.base.query.BaseQuery;
import cn.meizhu.base.service.impl.BaseServiceImpl;
import cn.meizhu.org.domain.Shop;
import cn.meizhu.org.mapper.ShopMapper;
import cn.meizhu.pet.domain.SearchMasterMsg;
import cn.meizhu.pet.mapper.PetMapper;
import cn.meizhu.pet.mapper.SearchMasterMsgMapper;
import cn.meizhu.pet.service.ISearchMasterMsgService;
import cn.meizhu.user.domain.LoginInfo;
import cn.meizhu.user.domain.User;
import cn.meizhu.user.mapper.UserMapper;
import cn.meizhu.util.DistanceUtil;
import cn.meizhu.util.LoginContext;
import cn.meizhu.util.Point;
import cn.meizhu.util.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public void publish(SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        System.out.println(searchMasterMsg);
        //设置默认领养状态
        searchMasterMsg.setState(0);
        //获取用户信息
        User user = (User) LoginContext.loginContextUser(request);
        System.out.println(user);


        searchMasterMsg.setUser_id(user.getId());
        //获取所有的商家信息
        List<Shop> shops = shopMapper.findAll();

        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());

        Shop shop = DistanceUtil.getNearestShop(point,shops);

        searchMasterMsg.setUser_id(shop.getId());
        super.save(searchMasterMsg);
    }

    @Override
    public void patchDelByIds(Long[] ids) {
        searchMasterMsgMapper.patchDelByIds(ids);
    }

    @Override
    public List<SearchMasterMsg> findAll(Long id,HttpServletRequest request) {

        LoginInfo loginInfo = (LoginInfo) LoginContext.loginContextUser(request);
        System.out.println(loginInfo.getId());

        return searchMasterMsgMapper.findAll(loginInfo.getId());
    }

    @Override
    public QueryResult<SearchMasterMsg> queryPage1(BaseQuery baseQuery) {
        Long count1 = searchMasterMsgMapper.queryCount1(baseQuery);
        List<SearchMasterMsg> data1 = searchMasterMsgMapper.queryData1(baseQuery);
        return new QueryResult<>(count1,data1);
    }


}
